<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/21
 * Time: 下午9:20
 */

namespace common\logic;


use common\models\RentingBrandModel;
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;

class RentingBrandLogic extends Logic
{

    /**
     * @return ActiveQuery
     */
    protected function _getActiveQuery()
    {
        return RentingBrandModel::find();
    }

    /**
     * @param $id
     *
     * @return array|null|BaseRegionModel
     */
    public function getById($id){
        return $this->_get(['id' => $id]);
    }

    public function getAll()
    {
        return $this->_all();
    }
	
	
	public function getPcBrand($page,$pageSize)
    {
    	$offset = ($page - 1) * $pageSize;
    	$Brand = $this->getQuery([],[],[],['id' => SORT_DESC])
	    	->limit($pageSize + 1)
	    	->offset($offset)
	    	->all();
        return $Brand;
    }
	
	public function getPcBrandNum()
    {
    	$Brand = $this->getQuery()->all();
        return count($Brand);
    }
	
	
	public function RentingBrandSave($model){
		//advicon 字段
		$this->saveFile($model, 'brandloge');
        $rs = $model->save();
        return $rs;
	}
	
	public function getByName($bname)
    {
    	$res = $this->getQuery(['bname' => $bname])->all();
        return $res;
    }
	
	public function getByNameId($bname,$id)
    {
    	$res = $this->getQuery(["bname"=>$bname])->andWhere("id<>$id")->all();
        return $res;
    }
	
	/**
     * 保存上传文件banner、广告位
     *
     * @param \common\models\HouseModel $model
     * @param                           $key
     */
    protected function saveFile(RentingBrandModel $model, $key)
    {
        if ($_FILES && !empty($_FILES['RentingBrandModel']['name'][$key])) {
            $filePath = "/upload/$key/";
            $fileName = $filePath . uniqid() . '.' . pathinfo($_FILES['RentingBrandModel']['name'][$key], PATHINFO_EXTENSION);
            $filePath = \Yii::getAlias('@webroot') . $filePath;
            if (!file_exists($filePath)) {
                mkdir($filePath, 0777, true);
            }
            $savePath = \Yii::getAlias('@webroot') . $fileName;
            if (move_uploaded_file($_FILES['RentingBrandModel']['tmp_name'][$key], $savePath)) {
                $model->$key = $fileName;
            }
        }
    }

}